home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Compression / Opener / Source / utils / arc / arc.c < prev    next >
C/C++ Source or Header  |  1993-08-04  |  12KB  |  426 lines

  1. /*
  2.  * $Header: arc.c,v 1.12 88/07/31 18:39:50 hyc Exp $
  3.  */
  4.  
  5. /*  ARC - Archive utility
  6.   
  7.     Version 5.21, created on 04/22/87 at 15:05:21
  8.   
  9. (C) COPYRIGHT 1985-87 by System Enhancement Associates; ALL RIGHTS RESERVED
  10.   
  11.     By:     Thom Henderson
  12.   
  13.     Description:
  14.      This program is a general archive utility, and is used to maintain
  15.      an archive of files.  An "archive" is a single file that combines
  16.      many files, reducing storage space and allowing multiple files to
  17.      be handled as one.
  18.   
  19.     Instructions:
  20.      Run this program with no arguments for complete instructions.
  21.   
  22.     Programming notes:
  23.      ARC Version 2 differs from version 1 in that archive entries
  24.      are automatically compressed when they are added to the archive,
  25.      making a separate compression step unecessary.     The nature of the
  26.      compression is indicated by the header version number placed in
  27.      each archive entry, as follows:
  28.   
  29.      1 = Old style, no compression
  30.      2 = New style, no compression
  31.      3 = Compression of repeated characters only
  32.      4 = Compression of repeated characters plus Huffman SQueezing
  33.      5 = Lempel-Zev packing of repeated strings (old style)
  34.      6 = Lempel-Zev packing of repeated strings (new style)
  35.      7 = Lempel-Zev Williams packing with improved hash function
  36.      8 = Dynamic Lempel-Zev packing with adaptive reset
  37.      9 = Dynamic Lempel-Zev packing, larger hash table
  38.   
  39.      Type 5, Lempel-Zev packing, was added as of version 4.0
  40.   
  41.      Type 6 is Lempel-Zev packing where runs of repeated characters
  42.      have been collapsed, and was added as of version 4.1
  43.   
  44.      Type 7 is a variation of Lempel-Zev using a different hash
  45.      function which yields speed improvements of 20-25%, and was
  46.      added as of version 4.6
  47.   
  48.      Type 8 is a different implementation of Lempel-Zev, using a
  49.      variable code size and an adaptive block reset, and was added
  50.      as of version 5.0
  51.   
  52.      Type 9 is a slight modification of type 8, first used by Phil
  53.      Katz in his PKARC utilites. The primary difference is the use
  54.      of a hash table twice as large as for type 8, and that this
  55.      algorithm called Squashing, doesn't perform run-length encoding
  56.      on the input data.
  57.   
  58.      Verion 4.3 introduced a temporary file for holding the result
  59.      of the first crunch pass, thus speeding up crunching.
  60.   
  61.      Version 4.4 introduced the ARCTEMP environment string, so that
  62.      the temporary crunch file may be placed on a ramdisk.    Also
  63.      added was the distinction bewteen Adding a file in all cases,
  64.      and Updating a file only if the disk file is newer than the
  65.      corresponding archive entry.
  66.   
  67.      The compression method to use is determined when the file is
  68.      added, based on whichever method yields the smallest result.
  69.   
  70.     Language:
  71.      Computer Innovations Optimizing C86
  72. */
  73. #include <stdio.h>
  74. #include "arc.h"
  75.  
  76. #if    UNIX
  77. #include <sys/types.h>
  78. #include <sys/stat.h>
  79. #endif
  80.  
  81. // Changed by Subrata Sircar to eliminate strlen() compiler warning
  82. //int        strlen();
  83. void        addarc(), delarc(), extarc(), lstarc(), tstarc(), cvtarc(), runarc();
  84. void        abort();
  85. static    void    expandlst();
  86. #if    MTS
  87. void        etoa();
  88. #endif
  89. #if    GEMDOS
  90. long        _stksize = 65536L;
  91. #endif
  92. char        *strcpy(), *strcat();
  93. char        *makefnam();    /* filename fixup routine */
  94.  
  95. static char   **lst;        /* files list */
  96. static int    lnum;        /* length of files list */
  97.  
  98. main(num, arg)            /* system entry point */
  99.     int        num;    /* number of arguments */
  100.     char           *arg[];    /* pointers to arguments */
  101. {
  102.     char        opt = 0;/* selected action */
  103.     char           *a;    /* option pointer */
  104.     void        upper();/* case conversion routine */
  105.     char           *index();/* string index utility */
  106.     char           *envfind();    /* environment searcher */
  107.     int        n;    /* index */
  108.     char           *arctemp2, *calloc(), *mktemp();
  109. #if    GEMDOS
  110.     void        exitpause();
  111.     int        append;
  112. #endif
  113. #if    MTS
  114.     fortran void    guinfo();
  115.     char        gotinf[4];
  116. #endif
  117. #if    UNIX
  118.     struct    stat    sbuf;
  119. #endif
  120.  
  121.     if (num < 3) {
  122.         printf("ARC - Archive utility, Version 5.21, created on 04/22/87 at 15:05:21\n");
  123. /*        printf("(C) COPYRIGHT 1985,86,87 by System Enhancement Associates;");
  124.         printf(" ALL RIGHTS RESERVED\n\n");
  125.         printf("Please refer all inquiries to:\n\n");
  126.         printf("       System Enhancement Associates\n");
  127.         printf("       21 New Street, Wayne NJ 07470\n\n");
  128.         printf("You may copy and distribute this program freely,");
  129.         printf(" provided that:\n");
  130.         printf("    1)     No fee is charged for such copying and");
  131.         printf(" distribution, and\n");
  132.         printf("    2)     It is distributed ONLY in its original,");
  133.         printf(" unmodified state.\n\n");
  134.         printf("If you like this program, and find it of use, then your");
  135.         printf(" contribution will\n");
  136.         printf("be appreciated.     You may not use this product in a");
  137.         printf(" commercial environment\n");
  138.         printf("or a governmental organization without paying a license");
  139.         printf(" fee of $35.  Site\n");
  140.         printf("licenses and commercial distribution licenses are");
  141.         printf(" available.  A program\n");
  142.         printf("disk and printed documentation are available for $50.\n");
  143.         printf("\nIf you fail to abide by the terms of this license, ");
  144.         printf(" then your conscience\n");
  145.         printf("will haunt you for the rest of your life.\n\n"); */
  146. #if    MSDOS
  147.         printf("Usage: ARC {amufdxerplvtc}[bswnoq][g<password>]");
  148. #endif
  149. #if    GEMDOS
  150.         printf("Usage: ARC {amufdxerplvtc}[bhswnoq][g<password>]");
  151. #endif
  152. #if    UNIX
  153.         printf("Usage: arc {amufdxerplvtc}[biswnoq][g<password>]");
  154. #endif
  155. #if    MTS
  156.         printf("Parameters: {amufdxeplvtc}[biswnoq][g<password>]");
  157. #endif
  158.         printf(" <archive> [<filename> . . .]\n");
  159.         printf("Where:     a   = add files to archive\n");
  160.         printf("     m   = move files to archive\n");
  161.         printf("     u   = update files in archive\n");
  162.         printf("     f   = freshen files in archive\n");
  163.         printf("     d   = delete files from archive\n");
  164.         printf("     x,e = extract files from archive\n");
  165. #if    !MTS
  166.         printf("     r   = run files from archive\n");
  167. #endif
  168.         printf("     p   = copy files from archive to");
  169.         printf(" standard output\n");
  170.         printf("     l   = list files in archive\n");
  171.         printf("     v   = verbose listing of files in archive\n");
  172.         printf("     t   = test archive integrity\n");
  173.         printf("     c   = convert entry to new packing method\n");
  174.         printf("     b   = retain backup copy of archive\n");
  175. #if    GEMDOS
  176.         printf("     h   = hold screen after finishing\n");
  177. #endif
  178. #if    MTS
  179.         printf("     i   = suppress ASCII/EBCDIC translation\n");
  180. #endif
  181. #if    UNIX
  182.         printf("     i   = suppress image mode (translate EOL)\n");
  183. #endif
  184.         printf("     s   = suppress compression (store only)\n");
  185.         printf("     w   = suppress warning messages\n");
  186.         printf("     n   = suppress notes and comments\n");
  187.         printf("     o   = overwrite existing files when");
  188.         printf(" extracting\n");
  189.         printf("     q   = squash instead of crunching\n");
  190.         printf("     g   = Encrypt/decrypt archive entry\n");
  191.         printf("\nAdapted from MSDOS by Howard Chu\n");
  192.         /*
  193.          * printf("\nPlease refer to the program documentation for");
  194.          * printf(" complete instructions.\n"); 
  195.          */
  196. #if    GEMDOS
  197.         exitpause();
  198. #endif
  199.         return 1;
  200.     }
  201.     /* see where temp files go */
  202. #if    !MTS
  203.     arctemp = calloc(1, STRLEN);
  204.     if (!(arctemp2 = envfind("ARCTEMP")))
  205.         arctemp2 = envfind("TMPDIR");
  206.     if (arctemp2) {
  207.         strcpy(arctemp, arctemp2);
  208.         n = strlen(arctemp);
  209.         if (arctemp[n - 1] != CUTOFF)
  210.             arctemp[n] = CUTOFF;
  211.     }
  212. #if    UNIX
  213.     else    strcpy(arctemp, "/tmp/");
  214. #endif
  215. #if    !MSDOS
  216.     {
  217.         static char tempname[] = "AXXXXXX";
  218.         strcat(arctemp, mktemp(tempname));
  219.     }
  220. #else
  221.     strcat(arctemp, "$ARCTEMP");
  222.     arctemp2 = NULL;
  223. #endif
  224. #else
  225.     guinfo("SHFSEP    ", gotinf);
  226.     sepchr[0] = gotinf[0];
  227.     guinfo("SCRFCHAR", gotinf);
  228.     tmpchr[0] = gotinf[0];
  229.     arctemp = "-$$$";
  230.     arctemp[0] = tmpchr[0];
  231. #endif
  232.  
  233. #if    !UNIX
  234.     /* avoid any case problems with arguments */
  235.  
  236.     for (n = 1; n < num; n++)    /* for each argument */
  237.         upper(arg[n]);    /* convert it to uppercase */
  238. #else
  239.     /* avoid case problems with command options */
  240.     upper(arg[1]);        /* convert to uppercase */
  241. #endif
  242.  
  243.     /* create archive names, supplying defaults */
  244. #if    UNIX
  245.     if (!stat(arg[2],&sbuf))
  246.         strcpy(arcname,arg[2]);
  247.     else
  248.         makefnam(arg[2],".arc",arcname);
  249. #else
  250.     makefnam(arg[2], ".ARC", arcname);
  251. #endif
  252.     /* makefnam(".$$$",arcname,newname); */
  253.     sprintf(newname, "%s.arc", arctemp);
  254.     makefnam(".BAK", arcname, bakname);
  255.  
  256.     /* now scan the command and see what we are to do */
  257.  
  258.     for (a = arg[1]; *a; a++) {    /* scan the option flags */
  259. #if    !MTS
  260.         if (index("AMUFDXEPLVTCR", *a)) {    /* if a known command */
  261. #else
  262.         if (index("AMUFDXEPLVTC", *a)) {
  263. #endif
  264.             if (opt)/* do we have one yet? */
  265.                 abort("Cannot mix %c and %c", opt, *a);
  266.             opt = *a;    /* else remember it */
  267.         } else if (*a == 'B')    /* retain backup copy */
  268.             keepbak = 1;
  269.  
  270.         else if (*a == 'W')    /* suppress warnings */
  271.             warn = 0;
  272. #if    !DOS
  273.         else if (*a == 'I')    /* image mode, no ASCII/EBCDIC x-late */
  274.             image = !image;
  275. #endif
  276. #if    GEMDOS
  277.         else if (*a == 'H')    /* pause before exit */
  278.             hold = 1;
  279. #endif
  280.  
  281.         else if (*a == 'N')    /* suppress notes and comments */
  282.             note = 0;
  283.  
  284.         else if (*a == 'O')    /* overwrite file on extract */
  285.             overlay = 1;
  286.  
  287.         else if (*a == 'G') {    /* garble */
  288.             password = a + 1;
  289.             while (*a)
  290.                 a++;
  291.             a--;
  292. #if    MTS
  293.             etoa(password, strlen(password));
  294. #endif
  295.         } else if (*a == 'S')    /* storage kludge */
  296.             nocomp = 1;
  297.  
  298.         else if (*a == 'K')    /* special kludge */
  299.             kludge = 1;
  300.  
  301.         else if (*a == 'Q')    /* use squashing */
  302.             dosquash = 1;
  303.  
  304.         else if (*a == '-' || *a == '/')    /* UNIX and PC-DOS
  305.                              * option markers */
  306.             ;
  307.  
  308.         else
  309.             abort("%c is an unknown command", *a);
  310.     }
  311.  
  312.     if (!opt)
  313.         abort("I have nothing to do!");
  314.  
  315.     /* get the files list set up */
  316.  
  317.     lnum = num - 3;        /* initial length of list */
  318.     lst = (char **) calloc((lnum==0) ? 1:lnum,
  319.                  sizeof(char *));    /* initial list */
  320.     for (n = 3; n < num; n++)
  321.         lst[n - 3] = arg[n];
  322.  
  323.     for (n = 0; n < lnum;) {/* expand indirect references */
  324.         if (*lst[n] == '@')
  325.             expandlst(n);
  326. #if    GEMDOS        /* redirect stdout from the desktop...*/
  327.         else if (*lst[n] == '>') {
  328.             arctemp2 = (++lst[n]);
  329.             lst[n] = lst[lnum-1];    /* delete this entry */
  330.             lnum--;
  331.             if (arctemp2[0] == '>') {
  332.                 append = 1;
  333.                 arctemp2++;
  334.             }
  335.             else    append = 0;
  336.         }
  337. #endif
  338.         else
  339.             n++;
  340.     }
  341. #if    GEMDOS
  342.     if (arctemp2)
  343.         freopen(arctemp2,append ? "a" : "w",stdout);
  344. #endif
  345.  
  346.     /* act on whatever action command was given */
  347.  
  348.     switch (opt) {        /* action depends on command */
  349.     case 'A':        /* Add */
  350.     case 'M':        /* Move */
  351.     case 'U':        /* Update */
  352.     case 'F':        /* Freshen */
  353.         addarc(lnum, lst, (opt == 'M'), (opt == 'U'), (opt == 'F'));
  354.         break;
  355.  
  356.     case 'D':        /* Delete */
  357.         delarc(lnum, lst);
  358.         break;
  359.  
  360.     case 'E':        /* Extract */
  361.     case 'X':        /* eXtract */
  362.     case 'P':        /* Print */
  363.         extarc(lnum, lst, (opt == 'P'));
  364.         break;
  365.  
  366.     case 'V':        /* Verbose list */
  367.         bose = 1;
  368.     case 'L':        /* List */
  369.         lstarc(lnum, lst);
  370.         break;
  371.  
  372.     case 'T':        /* Test */
  373.         tstarc();
  374.         break;
  375.  
  376.     case 'C':        /* Convert */
  377.         cvtarc(lnum, lst);
  378.         break;
  379. #if    !MTS
  380.     case 'R':        /* Run */
  381.         runarc(lnum, lst);
  382.         break;
  383. #endif
  384.     default:
  385.         abort("I don't know how to do %c yet!", opt);
  386.     }
  387. #if    GEMDOS
  388.     if (hold)
  389.         exitpause();
  390. #endif
  391.     return nerrs;
  392. }
  393. static    void
  394. expandlst(n)            /* expand an indirect reference */
  395.     int        n;    /* number of entry to expand */
  396. {
  397.     FILE           *lf, *fopen();    /* list file, opener */
  398.     char           *malloc(), *realloc();    /* memory managers */
  399.     char        buf[100];    /* input buffer */
  400.     int        x;    /* index */
  401.     char           *p = lst[n] + 1; /* filename pointer */
  402.  
  403.     if (*p) {        /* use name if one was given */
  404.         makefnam(p, ".CMD", buf);
  405.         if (!(lf = fopen(buf, "r")))
  406.             abort("Cannot read list of files in %s", buf);
  407.     } else
  408.         lf = stdin;    /* else use standard input */
  409.  
  410.     for (x = n + 1; x < lnum; x++)    /* drop reference from the list */
  411.         lst[x - 1] = lst[x];
  412.     lnum--;
  413.  
  414.     while (fscanf(lf, "%99s", buf) > 0) {    /* read in the list */
  415.         if (!(lst =(char **)realloc(lst, (lnum + 1) * sizeof(char *))))
  416.             abort("too many file references");
  417.  
  418.         lst[lnum] = malloc(strlen(buf) + 1);
  419.         strcpy(lst[lnum], buf); /* save the name */
  420.         lnum++;
  421.     }
  422.  
  423.     if (lf != stdin)    /* avoid closing standard input */
  424.         fclose(lf);
  425. }
  426.